package com.taotao.manage.controller;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.manage.pojo.ContentCategory;
import com.taotao.manage.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class ContentCategoryController {

    @Autowired
    private ContentCategoryService contentCategoryService;

    /**
     * 查询内容种类列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public List<EasyUITreeNode> getItemCatList(@RequestParam(value="id",defaultValue = "0")long parentId ){
        ContentCategory contentCategoryWhere = new ContentCategory();
        contentCategoryWhere.setParentId(parentId);
        List<ContentCategory> list = contentCategoryService.queryListByWhere(contentCategoryWhere);

        ArrayList<EasyUITreeNode> resultList = new ArrayList<>();
        for (ContentCategory contentCategory : list) {
            EasyUITreeNode node=new EasyUITreeNode();
            node.setId(contentCategory.getId());
            node.setText(contentCategory.getName());
            node.setState(contentCategory.getIsParent()?"closed":"open");
            node.setParentId(contentCategory.getParentId());
            resultList.add(node);
        }
        return resultList;
    }
    /**
     * 新增内容分类
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ContentCategory saveContentCategory(ContentCategory contentCategory){
        return contentCategoryService.saveContentCategory(contentCategory);
    }
    /**
     * 重命名内容分类
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public void updateContentCategory(ContentCategory contentCategory){
        contentCategoryService.updateSelective(contentCategory);
    }
    /**
     * 删除分类
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public void deleteContentCategory(ContentCategory contentCategory){
        contentCategoryService.deleteContentCategory(contentCategory);
    }
}
